package com.whfc.openapi.api.car.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.enums.DevicePlatform;
import com.whfc.common.enums.DeviceType;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.JSONUtil;
import com.whfc.openapi.api.car.service.OpenCarWashDataService;
import com.whfc.uni.entity.CarWashData;
import com.whfc.uni.entity.CarWashDevice;
import com.whfc.uni.service.CarWashDataService;
import com.whfc.uni.service.CarWashDeviceService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OpenCarWashDataServiceImpl implements OpenCarWashDataService {

    @DubboReference(interfaceClass = CarWashDataService.class, version = "1.0.0")
    private CarWashDataService carWashDataService;

    @DubboReference(interfaceClass = CarWashDeviceService.class, version = "1.0.0")
    private CarWashDeviceService carWashDeviceService;

    @Override
    public void record(String body) throws Exception {
        JSONObject jsonBody = JSONObject.parseObject(body);
        // 设备唯一标识
        String equipmentNo = jsonBody.getString("equipmentNo");
        CarWashDevice carWashDevice = carWashDeviceService.selectBySnAndPlatformAndType(equipmentNo, "defaults", DeviceType.carWash.getValue());
        if (carWashDevice == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "车辆冲洗设备不存在");
        }
        CarWashData carWashData = JSONUtil.parseObject(jsonBody.toJSONString(), CarWashData.class);
        carWashData.setDeviceId(carWashDevice.getId());
        carWashData.setCreateTime(new Date());
        carWashDataService.add(carWashData);
    }
}
